package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.file.NeuronJSONTools;
import com.iaz.tech.tools.uml.structural.pack.impl.NeuronPackage;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronPackageLoader extends NeuronJSONTools {
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronRealizationsLoader.class);
    
    /**
     * 解析JSON
     */
    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) {
       try {
            if (umlObj == null) {
                throw new Exception("umlObj 不能为空");
            }
            if (json == null) {
                throw new Exception("json 不能为空");
            }
            if (!(umlObj instanceof NeuronPackage)) {
                throw new Exception("umlObj 不是 NeuronRoler 类型"); 
            } 
            NeuronPackage package_struct = (NeuronPackage) umlObj;
            if (json.containsKey("path")) {
                package_struct.setPath(json.getString("path"));
            } else {
                throw new Exception("name 必须输入");
            }     
       } catch(Exception e) {
            logger.error("出现异常：" + e.getMessage());
       }

    }

    @Override
    public JSONObject toJSON(IUMLNeuronAble objStruct) {
        JSONObject result = new JSONObject();
        try {
            if (objStruct == null) {
                throw new Exception("要解析的UML对象为空"); 
            }
            if (!(objStruct instanceof NeuronPackage)){
                throw new Exception("要解析的UML对象不是NeuronPackage类型"); 
            }
            if (UtilTools.IsNull( ((NeuronPackage)objStruct).getPath())){
                throw new Exception("参数名称为空!");
            }
            result.put("path", ((NeuronPackage)objStruct).getPath());
        } catch (Exception e) {
            logger.error("解析JSON字符串失败", e); 
             result = new JSONObject();
        }
        return result;
    }
}
